package com.tmt.annotation.parser;

import com.tmt.helper.ReflectHelper;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * 该类用于读取字段上的FieldDescriptor注解的属性值。
 * Created by ThreeManTeam on 201711/14.
 */
public abstract class DescriptorParser<T extends Annotation> {

    private Class<T> annotationClazz;

    public DescriptorParser(Class<T> annotationClazz){
        this.annotationClazz = annotationClazz;
    }

    /**
     * 获取clazz类上的fieldName字段的FieldDescriptor注解的title属性值。
     * @param clazz
     * @param fieldName
     * @param defaultValue
     * @return
     */
    public String getTitle(Class<?> clazz, String fieldName, String defaultValue){
        Field target = ReflectHelper.findFieldByName(clazz, fieldName);
        String title = _getTitle(target);
        return title != null ? title : defaultValue;
    }

    /**
     * 获取clazz类上的fieldName字段的FieldDescriptor注解的description属性值。
     * @param clazz
     * @param fieldName
     * @param defaultValue
     * @return
     */
    public String getDescription(Class<?> clazz, String fieldName, String defaultValue){
        Field target = ReflectHelper.findFieldByName(clazz, fieldName);
        String description =  _getDescription(target);
        return description != null ? description : defaultValue;
    }

    /**
     * 获取clazz类上的fieldName字段的FieldDescriptor注解的order属性值。
     * @param clazz
     * @param fieldName
     * @return
     */
    public int getOrder(Class<?> clazz, String fieldName){
        Field target = ReflectHelper.findFieldByName(clazz, fieldName);
        return  _getOrder(target);
    }

    /**
     * 获取clazz类上的fieldName字段的FieldDescriptor注解的defaultSelected属性值。
     * @param clazz
     * @param fieldName
     * @return
     */
    public boolean getDefaultSelected(Class<?> clazz, String fieldName){
        Field target = ReflectHelper.findFieldByName(clazz, fieldName);
        return  _getDefaultSelected(target);
    }


    /***************************************************************************************************/
    /**********************私有方法*********************************************************************/

    private String _getTitle(Field target){
        if (target != null) {
            T[] array = target.getAnnotationsByType(annotationClazz);
            if (array.length > 0)
                return (String)ReflectHelper.invokeMethod(array[0], "title", null, null);
        }
        return null;
    }

    private String _getDescription(Field target){
        if (target != null) {
            T[] array = target.getAnnotationsByType(annotationClazz);
            if (array.length > 0)
                return (String)ReflectHelper.invokeMethod(array[0], "description", null, null);
        }
        return null;
    }

    private int _getOrder(Field target){
        if (target != null) {
            T[] array = target.getAnnotationsByType(annotationClazz);
            if (array.length > 0)
                return (int)ReflectHelper.invokeMethod(array[0], "order", null, null);
        }
        return 0;
    }

    private boolean _getDefaultSelected(Field target){
        if (target != null) {
            T[] array = target.getAnnotationsByType(annotationClazz);
            if (array.length > 0)
                return (boolean)ReflectHelper.invokeMethod(array[0], "defaultSelected", null, null);
        }
        return false;
    }

}
